package gepes.dao;

import gepes.exepcion.DAOExcepcion;
import gepes.modelo.Categoria;
import gepes.modelo.SubCategoria;
import gepes.util.ConexionBD;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

public class SubCategoriaDAO extends BaseDAO {
	public Collection<SubCategoria> listar(int id) throws DAOExcepcion {
		String query = "select codsubCategoria,nombre from subcategoria where codsubCategoria not in ("
				+ "select subcategoria_codcategoria from categoria_profesor where Profesor_codProfesor=?) and codcategoria in ("
				+ "select codcategoria from categoriaPadre_profesor where codProfesor=?)";
		Collection<SubCategoria> lista = new ArrayList<SubCategoria>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, id);
			stmt.setInt(2, id);
			
			rs = stmt.executeQuery();
			while (rs.next()) {
				SubCategoria pes = new SubCategoria();
				pes.setCodsubcategoria(rs.getInt(1));
				pes.setNombre(rs.getString(2));
				lista.add(pes);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {

			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		System.out.println(lista.size());
		return lista;
	}
	public Collection<SubCategoria> listar() throws DAOExcepcion {
		String query = "select codsubCategoria,nombre from subcategoria";
		Collection<SubCategoria> lista = new ArrayList<SubCategoria>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				SubCategoria pes = new SubCategoria();
				pes.setCodsubcategoria(rs.getInt(1));
				pes.setNombre(rs.getString(2));
				lista.add(pes);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {

			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		System.out.println(lista.size());
		return lista;
	}
	public int existe(String nombre) throws DAOExcepcion {
		String query = "select count(0) from subcategoria where nombre=?";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			stmt.setString(1, nombre);
			rs = stmt.executeQuery();
			if (rs.next()) {
				return rs.getInt(1);		
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {

			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return 0;
	}
	public SubCategoria insertar(SubCategoria sc) throws DAOExcepcion {
		String query = "insert into subcategoria(nombre,descripcion,codcategoria)"
				+ "values(?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.ConectarDB();
			stmt = con.prepareStatement(query);
			stmt.setString(1, sc.getNombre());
			stmt.setString(2, sc.getDescripcion());
			stmt.setInt(3, sc.getCodcategoria().getCodCategoria());

			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("Error insertar");
			}

			int id = 0;
			query = "select last_insert_id()";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			if (rs.next()) {
				id = rs.getInt(1);
			}
			sc.setCodsubcategoria(id);

		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return sc;
	}

}
